package controller;

import java.util.Vector;

import Client_Server.ClientConsole;
import Entity.Listg;
import Gui.ContactCustomer;


/**
 * The Class CreateListControl.Connects between the gui(CreateList) and the server.
 * Uses the entity listg and Customerlis.Creates the divided customer list
 *  and sends it to the the salesman.
 */
public class CreateListControl {
	
	/** The lst. */
	private Listg lst;
	
	/** The Deal id. */
	private String DealId;
	
	/** The listarr. */
	public static Listg[]  listarr = new Listg[1000];
	
	/** The o. */
	public int s=2;
	
	/** The list. */
	public static String[] list;
	
	/** The clc. */
	public static ContactCustomer clc;
	
	/**
	 * Constructor for new list, receives the dealid. create list for deal id.
	 *
	 * @param Did the did
	 */
	public CreateListControl(String Did){
		this.DealId=Did;
	}
	
	/**
	 *Empty Constructor for new list.
	 */
	public CreateListControl(){}
	
	/**
	 * Sends to the Clienconsel the deal -to the server(search for the deal in db) 
	 */
	public void submitCreateList(){
		String[] arr = new String[3];
		arr[0]="CreateCustomerList";
		arr[1]="dealdb";
		arr[2]=getDealID();
		ClientConsole.HandleMessageFromGui(arr);
	}
	
	/**
	 * Check if one of feilds are empty.
	 *
	 * @return true, if are not empty.
	 */
	public boolean checkdetail(){
		 if(getDealID().isEmpty()) //check if one of the fields empty
			 return false;
		 return true;
	}

	/**
	 * Gets the deal id.
	 *
	 * @return the deal id
	 */
	private String getDealID() {
		return DealId;
	}

	/**
	 * Sends to the Clienconsel a request to count number of salesman-to server
	 */
	public void conutSalesmen() {
		String[] arr = new String[2];
		arr[0]="CountSalesman";
		arr[1]="employeedb";
		ClientConsole.HandleMessageFromGui(arr);
		
	}

	/**
	 * Divide list between salesman and present list to the salesman logged in.
	 *
	 * @param listm the listm
	 * @param username the username
	 * @return the string[]
	 */
	
	@SuppressWarnings("static-access")
	public  String[] dividelist(String listm, String username) {
		int flag = 0, num=0;
		int flag1= 0, y=0;
		int flag2= 0;
		String[] one=listm.split("2:");
		String[] two=one[1].split("3:");
		String[] three=two[1].split("4: ");
		String[] sales=three[0].split(" ");

		String[] countsale=new String[2];

		countsale=three[1].split(": ");
		String[] cust=countsale[1].split(" ");
		String[] deal=one[0].split(":");
		
	
		int count=sales.length;
		count=(count-2)/3;//-----number of salesman

		int countcus=cust.length;
		list=new String[countcus];
		countcus=(countcus)/4;//----------number of customers
	
		int m=Integer.parseInt(countsale[0]);
		
		int numlist=0,n=2,k=0;
		flag1=count;
		numlist=countcus/count;
		if(m!=0) {
			k=numlist;
			m=(m*4)*numlist-2;
		System.out.println(m);
		}
		else { k=0;
			m=(m*4)*numlist;
		}

		
		if(countcus==1 && count==1) {//-------one customer one salesmen
			listarr[0].setdeal(deal[2]);
			listarr[0].settext(two[0]);
			listarr[0].setSalesmen(sales[n]+" "+sales[n+1]);
			clc.preslist="deal:"+listarr[0].getdeal()+" \n";
			list[0]=listarr[0].getdeal();
			clc.preslist+="text:\n"+listarr[0].gettext()+" \nCustomers to Contact:\n";
			list[1]=listarr[0].gettext();
			
			listarr[0].arrcus[0].setid(cust[m+k]);
			listarr[0].arrcus[0].setname(cust[m+k+1]+" "+cust[m+k+2]);
			listarr[0].arrcus[0].setPhone(cust[m+k+3]);
			
			clc.preslist+=listarr[0].arrcus[0].getid();
			list[s]=listarr[0].arrcus[0].getid();
			list[s+1]=listarr[0].arrcus[0].getname();
			clc.preslist+=" "+listarr[0].arrcus[0].getname();
			list[s+2]=listarr[0].arrcus[0].getPhone();
			clc.preslist+=" "+listarr[0].arrcus[0].getPhone();
			flag2=1;
		}
		if(count<countcus) {//------------------more salesmen then customers
		
			
				flag1=countcus;
				if (count==1)
					numlist=countcus;	
		}
		if(countcus==3 && m!=0)
			m++;
		
		

		if(flag2==0){//----------flag2==0 salesmen=2 and salesmen>customer
			for(int j=flag1; j>0 && sales.length> n; j-=numlist)

			{
				listarr[j].setdeal(deal[2]);
				listarr[j].settext(two[0]);
				listarr[j].setSalesmen(sales[n]+" "+sales[n+1]);
					n=n+3;
				if(listarr[j].getSalesmen().equals(username))
				{
					clc.preslist="Deal:"+listarr[j].getdeal()+" \n";
					list[0]=listarr[j].getdeal();
					clc.preslist+="Text:\n"+listarr[j].gettext()+" \nCustomers to Contact:\n";
					list[1]=listarr[j].gettext();
					flag=1;
					if(countcus%2==1 && n>2 && m!=0)//-----------if odd number of customer even number of sales and second sales to log in
						m+=4;
					if( count>2 && n>2 && m!=0)
						m++;
				
					if(countcus%2==1 && (j-numlist)==1)//if the number of customer is not an even number
						numlist++;

					for(int i=0; i<numlist;i++)
					{
						System.out.println(cust[m+k]);
						listarr[j].arrcus[i].setid(cust[m+k]);
						listarr[j].arrcus[i].setname(cust[m+k+1]+" "+cust[m+k+2]);
						listarr[j].arrcus[i].setPhone(cust[m+k+3]);
				
				
						if(flag==1)//----------if the salesman logged in matches the salesman of the list
						{
							clc.preslist+=listarr[j].arrcus[i].getid();
							list[s]=listarr[j].arrcus[i].getid();
							list[s+1]=listarr[j].arrcus[i].getname();
							clc.preslist+=" "+listarr[j].arrcus[i].getname();
							list[s+2]=listarr[j].arrcus[i].getPhone();
							clc.preslist+=" "+listarr[j].arrcus[i].getPhone();
							s=s+3;
						}
						m=m+3;
						k++;
					}
				}
				else
					if(countcus%2==1 && (j-numlist)==1)//if the number of customer is not an even number
						numlist++;
			}
		}
		return list;	
	}
}




